package cn.dzhang.rcp.hyperbola;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.ui.IWorkbenchPreferenceConstants;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.application.IWorkbenchConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchAdvisor;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.model.ContributionComparator;
import org.eclipse.ui.model.IContributionService;

import cn.dzhang.rcp.hyperbola.common.ResourceManager;
import cn.dzhang.rcp.hyperbola.debug.DebugConsole;

public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor {

	private static final String PERSPECTIVE_ID = "cn.dzhang.rcp.hyperbola.Perspective"; //$NON-NLS-1$

	@Override
	public void initialize(IWorkbenchConfigurer configurer) {
		configurer.setSaveAndRestore(true);

		IPreferenceStore pref = PlatformUI.getPreferenceStore();
		pref.setDefault(IWorkbenchPreferenceConstants.SHOW_PROGRESS_ON_STARTUP,
				true);
		pref.setDefault(IWorkbenchPreferenceConstants.DOCK_PERSPECTIVE_BAR,
				IWorkbenchPreferenceConstants.TOP_RIGHT);
		pref.setDefault(
				IWorkbenchPreferenceConstants.SHOW_TEXT_ON_PERSPECTIVE_BAR,
				false);
		pref.setDefault(
				IWorkbenchPreferenceConstants.SHOW_TRADITIONAL_STYLE_TABS,
				false);
		pref.setDefault(IWorkbenchPreferenceConstants.PERSPECTIVE_BAR_SIZE, 10);

		ConsolePlugin.getDefault().getConsoleManager()
				.addConsoles(new IConsole[] { new DebugConsole() });

		super.initialize(configurer);
	}

	@Override
	public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(
			IWorkbenchWindowConfigurer configurer) {
		return new ApplicationWorkbenchWindowAdvisor(configurer);
	}

	@Override
	public void preStartup() {
	}

	@Override
	public void postStartup() {
	}

	@Override
	public ContributionComparator getComparatorFor(String contributionType) {
		if (IContributionService.TYPE_PREFERENCE
				.equalsIgnoreCase(contributionType)) {
			return null;
		}

		return super.getComparatorFor(contributionType);
	}

	@Override
	public String getInitialWindowPerspectiveId() {
		return PERSPECTIVE_ID;
	}

	@Override
	public void postShutdown() {
		ResourceManager.dispose();
	}
}
